﻿using System;
using System.Windows;
using System.IO;
using System.Globalization;
using Journal.Helpers.Properties;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Google.GData.Calendar;
using log4net;
using log4net.Config;

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfiguratorAttribute(Watch = true, ConfigFile = "JournalApp.config")]

namespace Journal.Helpers
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class JournalApp : Application
    {
        public enum LogType { DebugLogger, FileLogger };

        public static string APP_DIR =
            System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "JournalApp";
        public static string USER_PROFILE = APP_DIR + Path.DirectorySeparatorChar + "profile";

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            GetLog(LogType.FileLogger).Debug("---------- APPLICATION STARTING ----------");
            GetLog(LogType.FileLogger).Debug("Creating application directory: " + APP_DIR);
            Utilities.CreateDirectory(APP_DIR);
            GetLog(LogType.FileLogger).Debug("Switching culture: " + CultureInfo.CurrentCulture.Name);
            Utilities.SwitchCulture(CultureInfo.CurrentCulture.Name);
        }

        public void Generate_schema()
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof(CalendarWindow).Assembly);
            new SchemaExport(cfg).Execute(false, false, false);
        }

        public static ILog GetLog(LogType logger)
        {
            return LogManager.GetLogger(logger.ToString());
        }

        private void Application_Exit(object sender, ExitEventArgs e)
        {
            GetLog(LogType.FileLogger).Debug("---------- APPLICATION EXITING ----------\n");
        }
    }
}
